//Baseball package
package baseball;

import java.lang.*;

public class Sabrmetrics{

	public Sabrmetrics(){
	}

	public double BaseRuns(int iAtBats, 
		int iHits,
		int iHR, 
		int iWalks, 
		int iIntentionalBaseOnBalls, 
		int iStolenBase, 
		int iCaughtStealing, 
		int iTotalBases)
	{
		double A = (double)(iHits + iWalks - iIntentionalBaseOnBalls - iCaughtStealing - iHR);
		double B = (double)((double)iTotalBases * 1.39 - (double)iHits * 0.58 - (double)iHR * 2.8 + (double)iWalks * 0.19 - (double)iIntentionalBaseOnBalls * 0.19 + (double)iStolenBase*1.2);
		double C = (double)iAtBats - (double)iHits;
		double D = (double)iHR;
	
		return A*B/(B+C) + D;
	}
	
	public double ComponentERA(int dwHits, 
		int dwHR, 
		int dwWalks, 
		int dwIWalks, 
		int dwHitBatter, 
		int dwTotalBattersFaced, 
		double dwInningsPitched)
	{
		double CER = (((dwHits - dwHR)*1.255 + dwHR * 4.0) * 0.89 + (dwWalks + dwIWalks + dwHitBatter) *0.56) *(dwHitBatter + dwWalks + dwHits)/dwTotalBattersFaced;
		return CER*9/dwInningsPitched -0.56;
	}
	
	public double EarnedRunAverage(int dwEarnedRuns, double dwInningsPitched)
	{
		return dwEarnedRuns * 9 / dwInningsPitched;
	}	
	
	public double ExtrapolatedRuns(int iAtBats, 
		int iHits, 
		int iDoubles, 
		int iTriples, 
		int iHR,  
		int iWalk,
		int iHBP, 
		int iBaseOnBalls, 
		int iIntentionalBaseOnBalls, 
		int iStolenBase, 
		int iCaughtStealing, 
		int iStrikeOuts, 
		int iGIDP, 
		int iSacrificeFlies, 
		int iSacrificeHits)
	{
		int iSingles = iHits - iHR - iDoubles - iTriples;
		
		return (double)iSingles * 0.5 + (double)iDoubles *0.72 + (double)iTriples * 1.04 + (double)iHR * 1.44 + 
			(double)(iHBP + iWalk - iIntentionalBaseOnBalls) * 0.34 + (double)iIntentionalBaseOnBalls * 0.25 + (double)iStolenBase * 0.18 -
			(double)iCaughtStealing * 0.32 - ((double)(iAtBats - iHits - iStrikeOuts)) * 0.09 - (double)iStrikeOuts * 0.098-
			(double)iGIDP * 0.37 + (double)iSacrificeFlies * 0.37 + (double)iSacrificeHits * 0.04;
	}
	
	public double IsolatedPower(int dwAtBats, 
		int dwDoubles, 
		int dwTriples, 
		int dwHR)
	{
		return ((double)(dwDoubles + 2*dwTriples + 3*dwHR))/(double)dwAtBats;
	}
	
	public double LinearWeights(int iAtBats, 
		int iHits, 
		int iDoubles, 
		int iTriples, 
		int iHR, 
		int iWalks, 
		int iHBP)
	{
		int iSingles = iHits - iDoubles - iTriples - iHR;
		return 0.47* (double)iSingles + 0.78*(double)iDoubles + 1.09*(double)iTriples + 1.4*(double)iHR + 0.33 * (iWalks + iHBP) - 0.3 * ((double)(iAtBats - iHits));
	}

	public double OffensiveWinningPercentage(int dwAtBats, 
		int dwHits, 
		int dwWalks, 
		int dwHBP, 
		int dwCaughtStealing, 
		int dwGIDP, 
		int dwTotalBases, 
		int dwSacrificeFlies, 
		int dwSacrificeHits, 
		int dwStolenBases, 
		int dwStrikeOuts, 
		double LeagueRunsPerGame)
	{
		double RC = 0.0;
	
		RC = RunsCreated(dwAtBats,
			dwHits,dwWalks,dwHBP,dwCaughtStealing,dwGIDP,
			dwTotalBases,dwSacrificeFlies,dwSacrificeHits,
			dwStolenBases,dwStrikeOuts)	;
	
		return Math.pow( (RC/27), 2)/(Math.pow( LeagueRunsPerGame, 2) + Math.pow( (RC/27), 2));
	}
	
	public double OnBasePercentage(
		int iAtBats, 
		int iHits, 
		int iWalks, 
		int iHBP,  
		int iSacrificeFlies, 
		int iSacrificeHits)
	{
		double A = (double)iHits + (double)iWalks + (double)iHBP;
		double B = (double)iAtBats + (double)iWalks + (double)iHBP + (double)iSacrificeFlies + (double)iSacrificeHits;
		return A/B;
	}
	
	public double OnBasePlusSlugging(
		int iAtBats, 
		int iHits, 
		int iDoubles, 
		int iTripples, 
		int iHR, 
		int iWalks, 
		int iHBP,  
		int iSacrificeFlies, 
		int iSacrificeHits)
	{
	
		double OBP = 0.0;
		double SLG = 0.0;
	
		OBP = OnBasePercentage(iAtBats,iHits, iWalks, iHBP, iSacrificeFlies, iSacrificeHits);
		SLG = SluggingPercentage(iAtBats, iHits, iDoubles,iTripples, iHR);

		return OBP + SLG;
	}
	
	public double PowerSpeedNumber(int dwHR, int dwStolenBase)
	{
		return ((double)(2 * dwHR * dwStolenBase))/((double)(dwHR + dwStolenBase));
	}
	
	public double PythagoreanWinningPercentage(int dwRunsScored, 
			int dwRunsAllowed)
	{
		return Math.pow( (double)dwRunsScored, 1.83)/(Math.pow( (double)dwRunsScored, 1.83) + Math.pow( (double)dwRunsAllowed, 1.83));
	}
	
	public double RunsCreated(
		int iAtBats, 
		int iHits, 
		int iWalks, 
		int iHBP, 
		int iSacrificeFlies, 
		int iSacrificeHits, 
		int iStolenBases, 
		int iCaughtStealing, 
		int iStrikeOuts,
		int iGIDP, 
		int iTotalBases)
	{
	
		double A = (double)(iHits + iWalks + iHBP - iCaughtStealing - iGIDP);
		double B =  (double)((double)iTotalBases + 0.26 * ((double)(iWalks + iHBP)) + 0.53 * ((double)(iSacrificeFlies + iSacrificeHits)) + 0.64 * iStolenBases - 0.03 * iStrikeOuts);
		double C = (double)(iAtBats + iWalks + iHBP + iSacrificeFlies + iSacrificeHits);
	
		return ((A+2.4*C)*(B+3*C))/(9*C) -0.9*C ;
	}	
	
	public double SecondaryAverage(
		int iAtBats, 
		int iDoubles, 
		int iTriples, 
		int iHR, 
		int iWalks, 
		int iStolenBase, 
		int iCaughtStealing)
	{
		return ((double)(iDoubles + 2 * iTriples + 3 * iHR + iWalks + iStolenBase - iCaughtStealing ))/(double)iAtBats;
	}
	
	public double SluggingPercentage(
		int iAtBats, 
		int iHits, 
		int iDoubles, 
		int iTripples, 
		int iHR)
	{
		return ((double)(iHits + iDoubles + 2 * iTripples + 3 * iHR))/(double)iAtBats;
	}

	public double WHIPRatio(int dwHits, 
		int dwWalks, 
		double InningsPitched)
	{
		return (dwHits + dwWalks)/ InningsPitched;
	}	
}

